IPython Command Line examples

IPython can be used to work with the system it's running on in pretty creative ways.

  • !command: run (and capture output) of a command
  • $ variables

In [ ]:
files = !ls -1
for file in files.grep('ipynb'):
    !echo $file

There are also some interesting 'magics' for working on shells, for example if you want to run some bash-specific code:


In [ ]:
%%bash

echo "Running under $BASH"
echo "Only Bash could do this..."
echo {a,b,c}{d,e,f}

Or even perl:


In [ ]:
%%perl

use DateTime;

print "The first of next month: ";
print DateTime->today()->add(months => 1)->set(day => 1)->ymd;

Runbooks

The basic capabilities of

  • Running commands
  • Displaying output
  • Being able to put comments around things

makes IPython Notebook great for quickly documenting "devops" processes.

For example, anything you can run via ssh you can quickly demonstrate, like finding the top recent account numbers referenced in one of our system logs:


In [11]:
!ssh ho "sudo head -2000 /var/log/mediatemple.log | awk '{print \$29}' | grep '[0-9]' | sort | uniq -c | sort -rn | head -10"


    180 60
     42 253084
     24 77458
     23 70
     18 65
     16 80
     16 152904
     15 258882
     14 252005
     12 84480

Even in a more analytics space than 'runbooks', that particular capability is useful.

Or, showing off a fabric task:


In [ ]:
!fab share:'Command Line Demo.ipynb'

awesome idea

In [ ]: